package yq.test.demo.User.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import yq.test.demo.Book.domain.Books;
import yq.test.demo.User.domain.User;
import yq.test.demo.User.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller

public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 返回登录页
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return "index";
    }


    /**
     * 跳转add页面并且实现add方法
     * * @return
     */
    @RequestMapping("Add")
    public ModelAndView Add(){
        return new ModelAndView("User/addUser");
    }
    @RequestMapping("addUser")
    public ModelAndView addUser(User user){
        userService.add(user);
        ModelAndView mav = new ModelAndView("redirect:/index");
        return mav;
    }

    /**
     *
     * 返回主页面
     * @return
     */
    @RequestMapping("home")
    public String home(){
        return "home";
    }

    /**
     * 错误页面
     * @return
     */
    @RequestMapping("err")
    public String Err(){
        return "User/Error_user";
    }

    /**
     *
     * 返回所有用户界面
     */
    @RequestMapping("/userAll")
    public String findAll(Model model){
        List<User> findAll= userService.findAll();
        model.addAttribute("all",findAll);
        return "User/user_all";
    }

    /***
     * 删除用户
     */
    @RequestMapping("deleteId/{id}")
    public ModelAndView deleteId(@PathVariable int id){
       userService.deleteId(id);
        ModelAndView mav = new ModelAndView("redirect:/userAll");
        return mav;
    }
    /*

    修改用户
     */
    @GetMapping("updateUser/{id}")
    public String updatePage(Model model,@PathVariable int id){
        User books = userService.get(id);
        model.addAttribute("book",books);
        return "User/UpUser";
    }
    @PostMapping("/updateUser")
    public String updateUser(Model model, User user, HttpServletRequest request){
        String id = request.getParameter("id");
        User userId= userService.get(Integer.parseInt(id));
        userService.update(user);
        System.out.println(user);
        return "redirect:/userAll";
    }

    /**
     *
     * 登陆验证
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "login",method = {RequestMethod.GET,RequestMethod.POST})
    public String loginId(HttpServletRequest request, HttpSession session){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User check = userService.loginId(username,password);
        if (check.equals(null)){
            return "redirect:";
        }else {
            return "redirect:home";
        }
    }
}
